/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-12-28
 * @Time  : 下午8:49
 * @brief :
 */

var Header = P.createUi({
    _data:{},
    _create:function(){
        var util = require('wap_common:widget/ui/libs/util.js').util,
            $sideBar = $('.side-bar').appendTo('body');

        this.data({
            '$header':$('.header'),
            '$sideBar':$sideBar,
            '$content':$('.side-bar .content')
        });

        $.get('/user/getusersidebar',function(ret,status){
            var amount = ret.data.wallet.total;
            $sideBar.find('.res-amount').text('￥'+util.formatAmount(amount,2));
        },'json');

    },

    _init:function(){
        var $sideBar = this.data('$sideBar'),
            $content = this.data('$content'),
            me = this;

        this.data('$header').find('.nav-btn').on("touchstart",function(){
            me.show();
        });

        $sideBar.on("touchstart",function(){
            me.hide();
        });

        $content.on("touchstart",function(e){
            e.stopPropagation();
        });

        $content.find('.nav li').on('touchstart',function(){
            var link = $(this).attr('data-href');
            $content.find('.nav li').removeClass('active');
            $(this).addClass('active');
            me.hide();
            setTimeout(function(){
                location.href=link;
            },500);
        });


    },

    show:function(){
        var $content = this.data('$content');
        this.data('$sideBar').show();
        this.data('$sideBar').find('.close-btn').show();
        setTimeout(function(){
            $content.css('-webkit-transform','translate(0,0)')
        },10)
    },

    hide:function(){
        var me = this;
        this.data('$sideBar').find('.close-btn').hide();
        this.data('$content').css('-webkit-transform','translate(200px,0)');
        setTimeout(function(){
            me.data('$sideBar').hide();
        },500);

    }
});

exports.header = function(option){
   return new Header(option);
};